﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AntikCompta.Data;

namespace AntikCompta
{
    public partial class BilanWnd : Form
    {
        private Exercice mExercice;
        private DataMng mDataMng;
        internal BilanWnd(ref DataMng aDataMng, ref Exercice aExercice)
        {
            mExercice = aExercice;
            mDataMng = aDataMng;
            InitializeComponent();

            mExercice.DataChanged += new DataChangedHandler(mExercice_DataChanged);
            CouleurLotMng.GetObject().ColorChanged += new DataChangedHandler(BilanWnd_ColorChanged);

            RefreshView();

            Text += " " + mExercice.Annee;
        }

        void BilanWnd_ColorChanged()
        {
            RefreshView();
        }

        void mExercice_DataChanged()
        {
            RefreshView();
        }

        private void RefreshView()
        {
            bilanView.Rows.Clear();
            List<Produit> liste;
            txt_status.Text = "";
            if (mDataMng.GetBilan(mExercice.Annee, out liste) == false)
                return;

            float tvaTot = 0;
            float achats = 0;
            float benefice = 0;

            if (liste.Count > 0)
            {
                bilanView.Rows.Add(liste.Count);

                int row = 0;
                while (row < liste.Count)
                {
                    bilanView.Rows[row].Cells[0].Value = liste[row].Reference;
                    bilanView.Rows[row].Cells[1].Value = liste[row].Nom;
                    bilanView.Rows[row].Cells[2].Value = liste[row].PrixAchat;
                    if( liste[row].DateAchat.Year == mExercice.Annee )
                        achats += liste[row].PrixAchat;

                    bilanView.Rows[row].Cells[3].Value = liste[row].CalculPrixVente(mExercice.Annee);

                    float tva = liste[row].CalculTva(mExercice.Annee);
                    bilanView.Rows[row].Cells[4].Value = tva;
                    tvaTot += tva;

                    float benef = liste[row].CalculBenefice(mExercice.Annee);
                    bilanView.Rows[row].Cells[5].Value = benef;
                    benefice += benef;

                    Color couleur;
                    if (CouleurLotMng.GetObject().GetColor(Produit.GetLotRef(liste[row].Reference), out couleur))
                        bilanView.Rows[row].DefaultCellStyle.BackColor = couleur;
                    
                    row++;
                }
            }
            txt_status.Text = "Bénéfice total: " + Math.Round(benefice, 2) + " € , TVA total: " + tvaTot + " € , total des achats " + mExercice.Annee + ": " + achats + " €";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void BilanWnd_FormClosing(object sender, FormClosingEventArgs e)
        {
            mExercice.DataChanged -= new DataChangedHandler(mExercice_DataChanged);
            CouleurLotMng.GetObject().ColorChanged -= new DataChangedHandler(BilanWnd_ColorChanged);
        }
    }
}
